using System.Configuration;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;
using System.IO;
using THSLib;
using THSERP.BusinessEntities;
namespace THSERP.BaseProvider
{
    partial class THSApp
    {

        public static String GetMainObjectNo(String strModuleName)
        {         
            String strMainObjectNo = String.Empty;
            GENumberingController objGENumberingController = new GENumberingController();
            GENumberingInfo objGENumberingInfo = (GENumberingInfo)new GENumberingController().GetObjectByName(strModuleName);
            if (objGENumberingInfo != null)
            {
                strMainObjectNo = String.Format("{0}{1}", objGENumberingInfo.GENumberingPrefix, objGENumberingInfo.GENumberingStart);
            }
            
            return strMainObjectNo;
        }

        public static void UpdateObjectNumbering(String strModuleName)
        {
            GENumberingController objGENumberingController = new GENumberingController();
            GENumberingInfo objGENumberingInfo = (GENumberingInfo)objGENumberingController.GetObjectByName(strModuleName);
            if (objGENumberingInfo != null)
            {
               
                objGENumberingInfo.GENumberingStart++;
                objGENumberingController.UpdateObject(objGENumberingInfo);
            }
        }

       
        public static void SwitchConnection(int iBranchID)
        {            
            SwitchConnection(iBranchID, false);
        }

        public static void SwitchConnectionNotCheckBranch(int iBranchID)
        {
            SwitchConnectionNotCheckBranch(iBranchID, false);
        }

        public static void SwitchConnection(int iBranchID, Boolean bDecrypt)
        {
            BRBranchsController branchController = new BRBranchsController();
            BRBranchsInfo objCurrentBranchsInfo = (BRBranchsInfo)branchController.GetObjectByID(iBranchID);

            if (objCurrentBranchsInfo != null)
            {
                if (THSApp.CurrentCompanyInfo != null)
                {
                    if (objCurrentBranchsInfo.BRBranchID != CurrentCompanyInfo.FK_BRBranchID)
                    {
                        String strDatabase = objCurrentBranchsInfo.BRBranchDatabase;
                        String strServer = objCurrentBranchsInfo.BRBranchServerName;
                        String strUser = objCurrentBranchsInfo.BRBranchDatabaseUser;
                        string strPassword = objCurrentBranchsInfo.BRBranchDatabasePassword;

                        if (bDecrypt)
                        {
                            EnCrypto cryp = new EnCrypto();
                            strPassword = cryp.Decrypt(strPassword);
                        }

                        String strBranchConnectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", strServer, strDatabase, strUser, strPassword);
                        SqlDbHelper.SwitchConnection(strBranchConnectionString);
                    }
                }                
            }        
        }

        public static void SwitchConnectionNotCheckBranch(int iBranchID, Boolean bDecrypt)
        {
            BRBranchsController branchController = new BRBranchsController();
            BRBranchsInfo objCurrentBranchsInfo = (BRBranchsInfo)branchController.GetObjectByID(iBranchID);

            if (objCurrentBranchsInfo != null)
            {
                //hieu 19/01/2012
                //if (THSApp.CurrentCompanyInfo != null)
                //{
                    //if (objCurrentBranchsInfo.BRBranchID != CurrentCompanyInfo.FK_BRBranchID)
                    //{
                        String strDatabase = objCurrentBranchsInfo.BRBranchDatabase;
                        String strServer = objCurrentBranchsInfo.BRBranchServerName;
                        String strUser = objCurrentBranchsInfo.BRBranchDatabaseUser;
                        string strPassword = objCurrentBranchsInfo.BRBranchDatabasePassword;

                        if (bDecrypt)
                        {
                            EnCrypto cryp = new EnCrypto();
                            strPassword = cryp.Decrypt(strPassword);
                        }

                        String strBranchConnectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", strServer, strDatabase, strUser, strPassword);
                        SqlDbHelper.SwitchConnection(strBranchConnectionString);
                    //}
                    //else
                      //  THSApp.RollbackLocalConnection();
                //}
            }
        }

        public static void RollbackLocalConnection()
        {
            SqlDbHelper.RollBackLocalConnection();
        }

        public static bool TestCurrentConnection()
        {
            return SqlDbHelper.TestConnection();
        }
    }
}